package com.encyclopedia.service.impl;

import com.encyclopedia.entity.Crop;
import com.encyclopedia.mapper.CropMapper;
import com.encyclopedia.result.exception.ServiceException;
import com.encyclopedia.result.exenum.RequestException;
import com.encyclopedia.service.CropService;
import com.encyclopedia.service.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CropServiceImpl implements CropService {
    @Autowired
    private CropMapper cropMapper;
    @Autowired
    private RedisUtils redisUtils;


    @Override
    public List<Crop> getCropAll() {
        String key = "CropServiceImplCropAll";
        List<Crop> cropVoRedis = (List<Crop>) redisUtils.get(key);
        System.out.println("-----------------------------------------------------");
        if (cropVoRedis != null) return cropVoRedis;
        List<Crop> cropAll = cropMapper.getCropAll();
        redisUtils.set(key, cropAll, 60 * 60 * 12);
        if (cropAll == null) throw new ServiceException(RequestException.NOT_FOUND);
        System.out.println(cropAll.toString());
        System.out.println("-----------------------------");
        return cropAll;
    }
}
